/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
jQuery.noConflict();
describe("startup", function() {
    beforeEach(function() {
        fileStub = jasmine.createSpyObj("fileApi", ['getServerDetails', 'getPassword', 'getServerDetails', 'setServerDetails', 'setPassword', 'setServerDetails','persist']);
        fsm = new ServiioStateMachine(fileStub);
    });
    it("should ask for server details on Startup if doesn't have info", function() {
        fileStub.getServerDetails.andCallFake(function() {
            return null;
        });
        fsm.startup();

        expect(fsm.current).toEqual("noDetails");

    });
    it("should login if have all details", function() {
        fsm.fileApi.getServerDetails.andCallFake(function() {
            return "10.1.1.1:8080";
        });
        fsm.fileApi.getPassword.andCallFake(function() {
            return "password";
        });
        fsm.fileApi.persist.andCallFake(function() {
        });
        spyOn(jQuery, "ajax").andCallFake(function(options) {
            var data = new Object();
            data.parameter = "authToken";
            options.success(data);
        });
        fsm.startup();
        expect(jQuery.ajax.mostRecentCall.args[0].url).toEqual("http://10.1.1.1:8080/cds/login");
        expect(jQuery.ajax.mostRecentCall.args[0].type).toEqual("POST");
        expect(fsm.current).toEqual("working");
        expect(fsm.authToken).toEqual("authToken");
    });
    it("should login if have all details, go to no details if fail", function() {
        fsm.fileApi.getServerDetails.andCallFake(function() {
            return "10.1.1.1:8080";
        });
        fsm.fileApi.getPassword.andCallFake(function() {
            return "password";
        });
        spyOn(jQuery, "ajax").andCallFake(function(options) {
            options.error();
        });
        fsm.startup();
        expect(jQuery.ajax.mostRecentCall.args[0].url).toEqual("http://10.1.1.1:8080/cds/login");
        expect(jQuery.ajax.mostRecentCall.args[0].type).toEqual("POST");
        expect(fsm.current).toEqual("noDetails");
        expect(fsm.authToken).toEqual(null);
    });
});
describe("noDetails", function() {
    beforeEach(function() {
        fileStub = jasmine.createSpyObj("fileApi", ['getServerDetails', 'getPassword', 'getServerDetails', 'setServerDetails', 'setPassword', 'setServerDetails','persist']);
        fsm = new ServiioStateMachine(fileStub);
        fsm.current = "noDetails";
    });
    it("should go back to starting an attempt to connect on setDetails", function() {

        fsm.fileApi.getServerDetails.andCallFake(function() {
            return "10.1.1.1:8080";
        });
        fsm.fileApi.getPassword.andCallFake(function() {
            return "password";
        });
        spyOn(jQuery, "ajax").andCallFake(function(options) {
            options.error();
        });
        fsm.setDetails(["10.1.1.1:8080", "password"]);
        expect(fsm.fileApi.setServerDetails).toHaveBeenCalledWith("10.1.1.1:8080");
        expect(fsm.fileApi.setPassword).toHaveBeenCalledWith("password");
        expect(jQuery.ajax.mostRecentCall.args[0].url).toEqual("http://10.1.1.1:8080/cds/login");
        expect(jQuery.ajax.mostRecentCall.args[0].type).toEqual("POST");
        expect(fsm.current).toEqual("noDetails");
        expect(fsm.authToken).toEqual(null);
    });
    it("should go back to starting then to working on success", function() {

        fsm.fileApi.getServerDetails.andCallFake(function() {
            return "10.1.1.1:8080";
        });
        fsm.fileApi.getPassword.andCallFake(function() {
            return "password";
        });
        fsm.fileApi.persist.andCallFake(function() {
        });
        spyOn(jQuery, "ajax").andCallFake(function(options) {
            var data = new Object();
            data.parameter = "authToken";
            options.success(data);
        });
        fsm.setDetails(["10.1.1.1:8080", "password"]);
        expect(jQuery.ajax.mostRecentCall.args[0].url).toEqual("http://10.1.1.1:8080/cds/login");
        expect(jQuery.ajax.mostRecentCall.args[0].type).toEqual("POST");
        expect(fsm.current).toEqual("working");
        expect(fsm.authToken).toEqual("authToken");
    });
});
describe("breadcrumb", function() {
    beforeEach(function() {
        fileStub = jasmine.createSpyObj("fileApi", ['getServerDetails', 'getPassword', 'getServerDetails', 'getBreadcrumb', 'setServerDetails', 'setPassword', 'setServerDetails','setBreadcrumb']);
        fsm = new ServiioStateMachine(fileStub);
        fsm.current = "working";
        fsm.serverDetails = "10.1.1.1:8080";
        fsm.authToken = 'token';
    });
    it("should go to breadcrumbSelect if it has a breadcrumb", function() {
        var element1 = {};
        element1.selectedIndex = 0;
        var option1_1 = new Folder();
        option1_1.title = "Video";
        option1_1.logicalFolder = 1;
        var option1_2 = new Folder();
        option1_2.title = "Music";
        option1_2.logicalFolder = 2;
        var option1_3 = new Folder();
        option1_3.title = "Image";
        option1_3.logicalFolder = 3;
        element1.options = [option1_1, option1_2, option1_3];
        element1.selecteIndex = 0;
        var element2 = {};
        var option2_1 = new Folder();
        option2_1.title = "Title";
        option2_1.logicalFolder = 4;
        var option2_2 = new Folder();
        option2_2.title = "Genre";
        option2_2.logicalFolder = 5;
        var option2_3 = new Folder();
        option2_3.title = "Artist";
        option2_3.logicalFolder = 6;
        element2.options = [option2_1, option2_2, option2_3];
        fsm.breadcrumb = [element1, element2];
        fsm.startContentSearch();
        expect(fsm.current).toEqual("breadcrumbSelect");
    });
    it("should go to breadCrumbSelect if it has a stored breadcrumb", function() {
        fsm.fileApi.getBreadcrumb.andCallFake(function() {
            return '[{"options":[{"title":"Video","logicalFolder":1},{"title":"Music","logicalFolder":2},{"title":"Images","logicalFolder":3}],"selectedIndex":0},{"options":[{"title":"Title","logicalFolder":4},{"title":"Genre","logicalFolder":5},{"title":"Artist","logicalFolder":6}]}]';
        });
        fsm.startContentSearch();
        expect(fsm.current).toEqual("breadcrumbSelect");
        var element1 = {};
        element1.selectedIndex = 0;
        var option1_1 = new Folder();
        option1_1.title = "Video";
        option1_1.logicalFolder = 1;
        var option1_2 = new Folder();
        option1_2.title = "Music";
        option1_2.logicalFolder = 2;
        var option1_3 = new Folder();
        option1_3.title = "Images";
        option1_3.logicalFolder = 3;
        element1.options = [option1_1, option1_2, option1_3];
        var element2 = {};
        var option2_1 = new Folder();
        option2_1.title = "Title";
        option2_1.logicalFolder = 4;
        var option2_2 = new Folder();
        option2_2.title = "Genre";
        option2_2.logicalFolder = 5;
        var option2_3 = new Folder();
        option2_3.title = "Artist";
        option2_3.logicalFolder = 6;
        element2.options = [option2_1, option2_2, option2_3];
        expect(fsm.breadcrumb).toEqual([element1, element2]);
        expect(fsm.selectionIndex).toEqual(1);
    });
    it("should start a search for breadcrumbs if it doesn't have one", function() {
        fsm.fileApi.getBreadcrumb.andCallFake(function() {
            return '';
        });
        spyOn(jQuery, "ajax").andCallFake(function(options) {
            // no=op
        });
        fsm.startContentSearch();
        expect(fsm.current).toEqual("breadcrumbWaitingOnResult");
    });
    it("should move to breadcrumb result", function() {
        fsm.fileApi.getBreadcrumb.andCallFake(function() {
            return '';
        });
        spyOn(jQuery, "ajax").andCallFake(function(options) {
            var data = {};
            data.returnedSize = 3;
            data.totalMatched = 3;
            var audio = new Object();
            audio.childCount = 12;
            audio.id = 'A';
            audio.parentId = '0';
            audio.title = 'Audio';
            audio.type = 'CONTAINER';
            var image = new Object();
            image.childCount = 7;
            image.id = 'I';
            image.parentId = '0';
            image.title = 'Images';
            image.type = 'CONTAINER';
            var video = new Object();
            video.childCount = 12;
            video.id = 'V';
            video.parentId = '0';
            video.title = 'Video';
            video.type = 'CONTAINER';
            data.objects = [audio, image, video];
            options.success(data);
        });
        fsm.startContentSearch();
        expect(fsm.current).toEqual("breadcrumbSelect");
        var audio = new Folder();
        audio.title = 'Audio';
        audio.logicalFolder = 'A';
        var image = new Folder();
        image.title = 'Images';
        image.logicalFolder = 'I';
        var video = new Folder();
        video.title = 'Video';
        video.logicalFolder = 'V';
        var item = {};
        item.options = [audio, image, video];
        item.selectionIndex = 0;
        expect(fsm.breadcrumb).toEqual([item]);
        expect(fsm.selectionIndex).toEqual(0);
    });
    it("should move to next folder in level down is fired into breadcrumbSelect", function() {
        var audio = new Folder();
        audio.title = 'Audio';
        audio.logicalFolder = 'A';
        var image = new Folder();
        image.title = 'Images';
        image.logicalFolder = 'I';
        var video = new Folder();
        video.title = 'Video';
        video.logicalFolder = 'V';
        var item = {};
        item.options = [audio, image, video];
        item.selectionIndex = 0;
        fsm.breadcrumb = [item];
        fsm.selectionIndex = 0;
        fsm.current = 'breadcrumbSelect';

        fsm.down();
        var expectedItem = {};
        expectedItem.options = [audio, image, video];
        expectedItem.selectionIndex = 1;
        expect(fsm.breadcrumb).toEqual([expectedItem]);
        expect(fsm.selectionIndex).toEqual(0);
    });
    it("should loop to first folder in level down is fired into breadcrumbSelect while at the end", function() {
        var audio = new Folder();
        audio.title = 'Audio';
        audio.logicalFolder = 'A';
        var image = new Folder();
        image.title = 'Images';
        image.logicalFolder = 'I';
        var video = new Folder();
        video.title = 'Video';
        video.logicalFolder = 'V';
        var item = {};
        item.options = [audio, image, video];
        item.selectionIndex = 2;
        fsm.breadcrumb = [item];
        fsm.selectionIndex = 0;
        fsm.current = 'breadcrumbSelect';

        fsm.down();
        var expectedItem = {};
        expectedItem.options = [audio, image, video];
        expectedItem.selectionIndex = 0;
        expect(fsm.breadcrumb).toEqual([expectedItem]);
        expect(fsm.selectionIndex).toEqual(0);
    });
    it("should loop to first folder in level when up is fired into breadcrumbSelect while at the end", function() {
        var audio = new Folder();
        audio.title = 'Audio';
        audio.logicalFolder = 'A';
        var image = new Folder();
        image.title = 'Images';
        image.logicalFolder = 'I';
        var video = new Folder();
        video.title = 'Video';
        video.logicalFolder = 'V';
        var item = {};
        item.options = [audio, image, video];
        item.selectionIndex = 0;
        fsm.breadcrumb = [item];
        fsm.selectionIndex = 0;
        fsm.current = 'breadcrumbSelect';

        fsm.up();
        var expectedItem = {};
        expectedItem.options = [audio, image, video];
        expectedItem.selectionIndex = 2;
        expect(fsm.breadcrumb).toEqual([expectedItem]);
        expect(fsm.selectionIndex).toEqual(0);
    });
    it("should loop to first folder in level when up is fired into breadcrumbSelect while at the end", function() {
        var audio = new Folder();
        audio.title = 'Audio';
        audio.logicalFolder = 'A';
        var image = new Folder();
        image.title = 'Images';
        image.logicalFolder = 'I';
        var video = new Folder();
        video.title = 'Video';
        video.logicalFolder = 'V';
        var item = {};
        item.options = [audio, image, video];
        item.selectionIndex = 2;
        fsm.breadcrumb = [item];
        fsm.selectionIndex = 0;
        fsm.current = 'breadcrumbSelect';

        fsm.up();
        var expectedItem = {};
        expectedItem.options = [audio, image, video];
        expectedItem.selectionIndex = 1;
        expect(fsm.breadcrumb).toEqual([expectedItem]);
        expect(fsm.selectionIndex).toEqual(0);
    });
    it("should start next selection on right", function() {
        var audio = new Folder();
        audio.title = 'Audio';
        audio.logicalFolder = 'A';
        var image = new Folder();
        image.title = 'Images';
        image.logicalFolder = 'I';
        var video = new Folder();
        video.title = 'Video';
        video.logicalFolder = 'V';
        var item = {};
        item.options = [audio, image, video];
        item.selectionIndex = 2;
        fsm.breadcrumb = [item];
        fsm.selectionIndex = 0;
        fsm.current = 'breadcrumbSelect';

        spyOn(jQuery, "ajax").andCallFake(function(options) {
            var data = {};
            data.returnedSize = 3;
            data.totalMatched = 3;
            var movies = new Object();
            movies.childCount = 12;
            movies.id = 'M';
            movies.parentId = 'V';
            movies.title = 'Movies';
            movies.type = 'CONTAINER';
            var folders = new Object();
            folders.childCount = 7;
            folders.id = 'F';
            folders.parentId = 'V';
            folders.title = 'Folders';
            folders.type = 'CONTAINER';
            var actors = new Object();
            actors.childCount = 12;
            actors.id = 'A1';
            actors.parentId = 'V';
            actors.title = 'Actors';
            actors.type = 'CONTAINER';
            data.objects = [movies, folders, actors];
            options.success(data);
        });

        fsm.right();

        expect(jQuery.ajax.mostRecentCall.args[0].url).toEqual("http://10.1.1.1:8080/cds/browse/flv_player/V/BrowseDirectChildren/containers/0/0?authToken=token");
        var expectedItem = {};
        expectedItem.options = [audio, image, video];
        expectedItem.selectionIndex = 2;
        var movies = new Folder();
        movies.title = 'Movies';
        movies.logicalFolder = 'M';
        var folders = new Folder();
        folders.title = 'Folders';
        folders.logicalFolder = 'F';
        var actors = new Folder();
        actors.title = 'Actors';
        actors.logicalFolder = 'A1';
        var expectedItem2 = {};
        expectedItem2.options = [movies, folders, actors];
        expectedItem2.selectionIndex = 0;
        expect(fsm.breadcrumb).toEqual([expectedItem, expectedItem2]);
        expect(fsm.selectionIndex).toEqual(1);
    });
    it("should not add to breadcrumb if there are no containers in the new search", function() {
        var audio = new Folder();
        audio.title = 'Audio';
        audio.logicalFolder = 'A';
        var image = new Folder();
        image.title = 'Images';
        image.logicalFolder = 'I';
        var video = new Folder();
        video.title = 'Video';
        video.logicalFolder = 'V';
        var item = {};
        item.options = [audio, image, video];
        item.selectionIndex = 2;
        fsm.breadcrumb = [item];
        fsm.selectionIndex = 0;
        fsm.current = 'breadcrumbSelect';

        spyOn(jQuery, "ajax").andCallFake(function(options) {
            var data = {};
            data.returnedSize = 3;
            data.totalMatched = 3;
            var movies = new Object();
            movies.childCount = 12;
            movies.id = 'M';
            movies.parentId = 'V';
            movies.title = 'Movies';
            movies.type = 'ITEM';
            var folders = new Object();
            folders.childCount = 7;
            folders.id = 'F';
            folders.parentId = 'V';
            folders.title = 'Folders';
            folders.type = 'ITEM';
            var actors = new Object();
            actors.childCount = 12;
            actors.id = 'A1';
            actors.parentId = 'V';
            actors.title = 'Actors';
            actors.type = 'ITEM';
            data.objects = [movies, folders, actors];
            options.success(data);
        });

        fsm.right();

        expect(jQuery.ajax.mostRecentCall.args[0].url).toEqual("http://10.1.1.1:8080/cds/browse/flv_player/V/BrowseDirectChildren/containers/0/0?authToken=token");
        var expectedItem = {};
        expectedItem.options = [audio, image, video];
        expectedItem.selectionIndex = 2;
        expect(fsm.breadcrumb).toEqual([expectedItem]);
        expect(fsm.selectionIndex).toEqual(0);
    });
    it("should delete the trailing part of the breadcrumb on left", function() {
        var audio = new Folder();
        audio.title = 'Audio';
        audio.logicalFolder = 'A';
        var image = new Folder();
        image.title = 'Images';
        image.logicalFolder = 'I';
        var video = new Folder();
        video.title = 'Video';
        video.logicalFolder = 'V';
        var item = {};
        item.options = [audio, image, video];
        item.selectionIndex = 2;
        var movies = new Folder();
        movies.title = 'Movies';
        movies.logicalFolder = 'M';
        var folders = new Folder();
        folders.title = 'Folders';
        folders.logicalFolder = 'F';
        var actors = new Folder();
        actors.title = 'Actors';
        actors.logicalFolder = 'A1';
        var item2 = {};
        item2.options = [movies, folders.actors];
        item2.selectionIndex = 0;
        fsm.breadcrumb = [item, item2];
        fsm.selectionIndex = 1;
        fsm.current = 'breadcrumbSelect';

        fsm.left();

        var expectedItem = {};
        expectedItem.options = [audio, image, video];
        expectedItem.selectionIndex = 2;
        expect(fsm.breadcrumb).toEqual([expectedItem]);
        expect(fsm.selectionIndex).toEqual(0);
    });
    it("should leave breadcrumb if at first item on left", function() {
        var audio = new Folder();
        audio.title = 'Audio';
        audio.logicalFolder = 'A';
        var image = new Folder();
        image.title = 'Images';
        image.logicalFolder = 'I';
        var video = new Folder();
        video.title = 'Video';
        video.logicalFolder = 'V';
        var item = {};
        item.options = [audio, image, video];
        item.selectionIndex = 2;
        fsm.breadcrumb = [item];
        fsm.selectionIndex = 0;
        fsm.current = 'breadcrumbSelect';

        fsm.left();

        var expectedItem = {};
        expectedItem.options = [audio, image, video];
        expectedItem.selectionIndex = 2;
        expect(fsm.breadcrumb).toEqual([expectedItem]);
        expect(fsm.selectionIndex).toEqual(0);
    });
    it("should move to initialContentSearch after select at the current breadcrumb", function() {
        var audio = new Folder();
        audio.title = 'Audio';
        audio.logicalFolder = 'A';
        var image = new Folder();
        image.title = 'Images';
        image.logicalFolder = 'I';
        var video = new Folder();
        video.title = 'Video';
        video.logicalFolder = 'V';
        var item = {};
        item.options = [audio, image, video];
        item.selectionIndex = 2;
        fsm.breadcrumb = [item];
        fsm.selectionIndex = 0;
        fsm.current = 'breadcrumbSelect';
        spyOn(jQuery, "ajax").andCallFake(function(options) {
            var data = {};
            data.returnedSize = 3;
            data.totalMatched = 3;
            var movies = new Object();
            movies.childCount = 12;
            movies.id = 'M';
            movies.parentId = 'V';
            movies.title = 'Movies';
            movies.type = 'ITEM';
            var folders = new Object();
            folders.childCount = 7;
            folders.id = 'F';
            folders.parentId = 'V';
            folders.title = 'Folders';
            folders.type = 'ITEM';
            var actors = new Object();
            actors.childCount = 12;
            actors.id = 'A1';
            actors.parentId = 'V';
            actors.title = 'Actors';
            actors.type = 'ITEM';
            data.objects = [movies, folders, actors];
            options.success(data);
        });
        
        
        fsm.select();
        expect(jQuery.ajax.mostRecentCall.args[0].url).toEqual("http://10.1.1.1:8080/cds/browse/flv_player/V/BrowseDirectChildren/containers/0/0?authToken=token");
        //expect(fileStub.setBreadcrumb).toHaveBeenCalledWith([item]);
        expect(fsm.current).toEqual("selectContent");
    });
});

